<template>
	<view class="todo-audit">
		<view class="title">签署标题：{{ audit.title }}</view>
		<view class="todo-audit-item">
			<view class="item">签署人：{{ audit.name || '-' }}</view>
			<view class="item">电话：{{ audit.phone }}</view>
		</view>

		<view class="todo-audit-item">
			<view class="item">时间：{{ formatISO(audit.createTime) }}
			</view>
			<view class="item">发起人：{{ audit?.member?.name || '-' }}</view>
		</view>

		<view class="line"></view>

		<view class="btns">

			<view class="btn-item preview" @click="handlePreviewe(audit.fileId)">
				<uni-icons type="contact" size="14" color="#409EFF"></uni-icons>
				查看合同
			</view>

			<view class="btn-item pass" @click="handlePass(audit)">
				<uni-icons type="checkmarkempty" size="14" color="#67C23A"></uni-icons>
				同意
			</view>

			<view class="btn-item delete" @click="handleDel(audit)">
				<uni-icons type="close" size="14" color="#F56C6C"></uni-icons>
				驳回
			</view>
		</view>

		<up-modal title="驳回理由" :show="show" @confirm="confirm" ref="uModal" showCancelButton @cancel="show = false">
			<template #default>
				<uni-easyinput v-model="message" placeholder="请输入驳回理由" />
			</template>
		</up-modal>
	</view>
</template>

<script>
import {
	formatISODateTime
} from '@/utils/index.js'
import {
	updateContrat,
	getTemplateDC,
	sendTemplateDC,
	getPDF,
} from '@/api/app.js'
export default {
	name: 'TodoAudit',
	props: {
		audit: {
			type: Object,
			default: {}
		}
	},
	data() {
		return {
			message: "",
			positions: [],
			show: false,
			rejectModel: {}
		}
	},
	methods: {
		// 
		formatISO(time) {
			return formatISODateTime(time)
		},
		// 
		isBool(str) {
			return str.includes('个人章')
		},
		// 查看合同
		async handlePreviewe(docTemplateId) {
			const res = await getPDF({
				docTemplateId
			})




			if (res && res.code == 0) {
				const url = res.data.fileDownloadUrl
				// uni.setStorageSync('pdfUrl', res.data.fileDownloadUrl)
				// uni.navigateTo({
				// 	url: `/pages/previewPDF/index`
				// })
				uni.downloadFile({
					url,
					success: function (res) {
						let filePath = res.tempFilePath;
						uni.openDocument({
							filePath: filePath,
							showMenu: true,
							success: function (res) {
								console.log('打开文档成功', res);
							}
						});
					}
				});
			} else {
				uni.showToast({
					icon: 'none',
					title: '获取合同失败',
					duration: 2000
				})
			}

		},
		// 同意
		async handlePass(item) {
			uni.showModal({
				title: '提示',
				content: '是否通过此合同？',
				success: async (res) => {
					console.log(res);
					if (res.confirm) {
						await this.getDC(item.pdfId)
						let shdata = {
							psnAccount: item.phone,
							psnName: item.name,
							fileId: item.fileId,
							signFlowTitle: item.title,
							positionss: this.positions,
							phone: item.member.mobile
						}

						const res = await sendTemplateDC(shdata)
						if (res.code == 0) {
							delete item.member
							item.Status = true
							item.signFlowId = res.data.signFlowId
							const resp = await updateContrat(item)
							if (resp.code == 1) {
								uni.showToast({
									icon: 'none',
									title: '通过成功',
									duration: 2000
								})
								this.$emit('init')
							}
						}
					}
				}
			})

		},

		async confirm() {
			delete this.rejectModel.member
			this.rejectModel.delete = true
			this.rejectModel.message = this.message
			console.log(this.rejectModel);
			const resp = await updateContrat(this.rejectModel)
			if (resp.code == 1) {
				this.show = false
				uni.showToast({
					icon: 'none',
					title: '驳回成功',
					duration: 2000
				})

				setTimeout(() => {
					this.$emit('init')
				}, 1500)
			}
		},
		// handleDel
		handleDel(item) {
			uni.showModal({
				title: '提示',
				content: '是否驳回此合同？',
				success: async (res) => {
					console.log(item);
					const itemObj = item
					if (res.confirm) {
						this.show = true
						// delete itemObj.member
						// itemObj.delete = true
						this.rejectModel = itemObj
					}
				}
			})
		},
		async getDC(fileId) {
			const res = await getTemplateDC(fileId)
			res.data.components.forEach(item => {
				if (this.isBool(item.componentName)) {
					this.positions.push({
						PositionPage: item.componentPosition.componentPageNum,
						positionX: item.componentPosition.componentPositionX,
						positionY: item.componentPosition.componentPositionY
					})
				}
			})
		}
	}
}
</script>

<style scoped lang="scss">
.todo-audit {
	margin-bottom: 20rpx;
	position: relative;
	padding: 40rpx 20rpx 20rpx;
	background-color: #ffffff;
	border-radius: 13rpx;
	box-shadow: rgba(100, 100, 111, 0.2) 0px 7rpx 29rpx 0px;

	.title {
		font-size: 26rpx;
		font-weight: bold;
		margin-bottom: 10rpx;
	}

	.todo-audit-item {
		display: flex;
		align-items: center;
		justify-content: space-between;
		margin-bottom: 10rpx;

		.item {
			flex: 1;
			font-size: 24rpx;
			color: '#666666'
		}
	}

	.line {
		width: 100%;
		height: 1rpx;
		color: '#cccccc'
	}

	.btns {
		margin-top: 20rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;

		.btn-item {
			flex: 1;
			font-size: 26rpx;
			height: 60rpx;
			line-height: 60rpx;
			text-align: center;

		}

		.delete {
			color: #F56C6C;
		}

		.pass {
			color: #67C23A;
		}

		.preview {
			color: '#409EFF';
		}
	}
}
</style>